package com.wswy.wzcx.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.che.libcommon.ui.BaseActivity;
import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.R;

/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public abstract class IFragmentActivity extends BaseActivity implements BaseFragment.FragmentCallBack {
    public static final String EXTRA_TARGET_NAME = "extra.target_name";
    public static final String EXTRA_ARGS_PARAM = "extra.arg_param";
    public static final String EXTRA_HIDE_TITLE = "extra.ui.hidetitle";
    protected BaseFragment fragment = null;
    TextView tvTitle;
    View back;
    public Toolbar toolbar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine);

        tvTitle = findViewById(R.id.tv_title);
        back = findViewById(R.id.img_back);
        toolbar = findViewById(R.id.toolbar);

        initContent();
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragment != null && fragment.isAdded()){
                    if(!fragment.onBackPress()){
                        finish();
                    }
                }
            }
        });
    }

    String param;

    public String getParam() {
        return param;
    }

    protected void initContent() {
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra(EXTRA_TARGET_NAME)) {
            String stringExtra = intent.getStringExtra(EXTRA_TARGET_NAME);
            if (intent.hasExtra(EXTRA_ARGS_PARAM)) {
                param = intent.getStringExtra(EXTRA_ARGS_PARAM);
            }
            fragment = getFragmentFromExtra(stringExtra);
            if (fragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.content_frame, fragment)
                        .commit();
                View view = fragment.getTitleRightView(this);
                if (view != null){
                    FrameLayout frameLayout = findViewById(R.id.fl_right_container);
                    frameLayout.addView(view);
                }
            }
            if (intent.getBooleanExtra(EXTRA_HIDE_TITLE,false)){
                findViewById(R.id.appbar).setVisibility(View.GONE);
            }else {
                configToolBar();
            }
            invalidateOptionsMenu();
        } else {
            finish();
        }
    }

    protected abstract BaseFragment getFragmentFromExtra(String stringExtra);

    public void configToolBar() {

    }

    @Override
    public void configTitle(String title) {
        if (TextUtils.isEmpty(title)) return;
        tvTitle.setText(title);
    }

    @Override
    public void onBackPressed() {
        if(fragment != null && fragment.isAdded()){
            if(!fragment.onBackPress()){
                super.onBackPressed();
            }
        }else {
            super.onBackPressed();
        }
    }
}
